/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.controller;

import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.vng.zing.survey.model.CommonModel;
import com.vng.zing.survey.model.RenderModel;
import com.vng.zing.survey.thrift.model.UserType;
import java.util.Map;

/**
 *
 * @author namhh
 */
public class NewSurveyController extends BaseController {

    private static UserType userType = UserType.ADMIN;

    @Override
    public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {
        String content = "";
        TemplateDataDictionary dic = TemplateDictionary.create();

        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Tạo khảo sát mới"));

        Map mapUser = checkMember(req, userType);
        if (mapUser.containsValue(ErrorType.FAIL) || mapUser.containsValue(ErrorType.ERROR_SYSTEM)) {
            return CommonModel.getError(dic, (String) mapUser.get("msg"));
        }
        boolean isAdmin = (boolean) mapUser.get("admin");
        if(!isAdmin){
             return CommonModel.getError(dic, "Bạn không phải admin");
        
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

        Calendar cal = Calendar.getInstance();
        System.out.println(dateFormat.format(cal.getTime()));
        dic.setVariable("DATESTART", dateFormat.format(cal.getTime()));
        cal.add(Calendar.DATE, 7);
        dic.setVariable("DATEEND", dateFormat.format(cal.getTime()));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));

        Template template = RenderModel.getCTemplate("newsurvey");
        content = template.renderToString(dic);

        return content;
    }
}
